import { createSlice } from "@reduxjs/toolkit";

const drugsMassage = createSlice({
    name: 'drugs-massage',
    initialState: {
        drugsList:[]
    },
    reducers: {
        getDrugsData:(state, action)=>{
            state.drugsList=action.payload
            localStorage.setItem('drugsList', JSON.stringify(state.drugsList))
        },
        getDrugsListDel: (state, action) => {
            state.drugsList = state.drugsList.filter((v: { id: number; }) => v.id !== action.payload)
            localStorage.setItem('bedlist', JSON.stringify(state.drugsList))
        },
        getAddDrugsList: (state, action) => {
            state.drugsList.concat({
                id: state.drugsList.length,
                drugsname: action.payload.drugsname,
                sort: action.payload.sort,
                img: action.payload.img,
                type: action.payload.type,
                count: action.payload.count,
                price: action.payload.price,
                starttime: action.payload.starttime,
                endtime: action.payload.endtime,
            })
            localStorage.setItem('drugsList', JSON.stringify(state.drugsList))
        },
        getEditDrugsList: (state, action) => {
            const index = state.drugsList.findIndex((item: { id: any; }) => item.id === action.payload.id)
            if (index !== -1) {
                state.drugsList[index] = {
                    ...state.drugsList[index],
                    drugsname: action.payload.drugsname,
                    sort: action.payload.sort,
                    img: action.payload.img,
                    type: action.payload.type,
                    count: action.payload.count,
                    price: action.payload.price,
                    starttime: action.payload.starttime,
                    endtime: action.payload.endtime,
                }
            }
            localStorage.setItem('bedlist', JSON.stringify(state.drugsList))
        },
        getSearchDrugs: (state, action) => {
            if (action.payload.drugsname !== '' && action.payload.sort !== '') {
                state.drugsList = state.drugsList.filter((v: any) => v.drugsname === action.payload.drugsname && v.sort === action.payload.sort)
            }
            if (action.payload.drugsname !== '' || action.payload.sort !== '') {
                state.drugsList = state.drugsList.filter((v: any) => v.drugsname === action.payload.drugsname || v.sort === action.payload.sort)
            }
            if (action.payload.drugsname === '' && action.payload.sort === '') {
                state.drugsList = JSON.parse((localStorage as any).getItem('drugsList'))
            }
        },
        getcountSub: (state, action) => {
            const index = state.drugsList.findIndex((item: { id: any; }) => item.id === action.payload);
            if (index !== -1 && state.drugsList[index].count > 0) {
                state.drugsList[index].count -= 1;
            }
            localStorage.setItem('drugsList', JSON.stringify(state.drugsList));
        },
        getcountAdd: (state, action) => {
            const index = state.drugsList.findIndex((item: { id: any; }) => item.id === action.payload);
            if (index !== -1) {
                state.drugsList[index].count += 1;
            }
            localStorage.setItem('drugsList', JSON.stringify(state.drugsList));
        },
        getpriceSub: (state, action) => {
            const index = state.drugsList.findIndex((item: { id: any; }) => item.id === action.payload);
            if (index !== -1 && state.drugsList[index].price > 0) {
                state.drugsList[index].price -= 1;
            }
            localStorage.setItem('drugsList', JSON.stringify(state.drugsList));
        },
        getpriceAdd: (state, action) => {
            const index = state.drugsList.findIndex((item: { id: any; }) => item.id === action.payload);
            if (index !== -1) {
                state.drugsList[index].price += 1;
            }
            localStorage.setItem('drugsList', JSON.stringify(state.drugsList));
        }
    }
});

export const { getDrugsData,getDrugsListDel, getAddDrugsList, getEditDrugsList, getSearchDrugs, getcountSub, getcountAdd,getpriceSub,getpriceAdd } = drugsMassage.actions;
export default drugsMassage.reducer;